package com.design.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.reggie.common.CustomException;
import com.design.reggie.entity.Category;
import com.design.reggie.entity.Dish;
import com.design.reggie.entity.Setmeal;
import com.design.reggie.service.CategoryService;
import com.design.reggie.mapper.CategoryMapper;
import com.design.reggie.service.DishService;
import com.design.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author JPY
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-01-30 10:49:20
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(long id) {

        //查询当前菜品分类下是否还有菜品，如果有菜品就不能删除，抛出异常打断
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询条件，查看菜品id与分类id相等的数量
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        // 执行查询
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        if (dishCount > 0) {
            // 已经关联菜品，抛出业务异常
            throw new CustomException("当前分类下关联了菜品，无法删除");
        }

        //查询当前菜品分类是否关联了套餐，如果有套餐就不能删除，抛出异常打断
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setmealCount = setmealService.count(setmealLambdaQueryWrapper);
        if (setmealCount > 0) {
            // 已经关联套餐，抛出业务异常
            throw new CustomException("当前分类下关联了套餐，无法删除");
        }

        // 正常删除
        super.removeById(id);

    }
}




